package jsp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/13 14:07
 * Version:1.0-SNAPSHOT
 * Description:
 */
@WebServlet("/count")
public class CountServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer count = (Integer) request.getSession().getAttribute("count");
        // 当第一次访问的时候,当前session中并没有count的数据
        // 此时可以设置一个初始值1
        if(null == count){
            count = 1;
        } else{
            // 如果取到了数据,则说明不是第一次访问
            // 可以在原有数值的基础上+1
            count++;
        }
        request.getSession().setAttribute("count",count++);
        request.getRequestDispatcher("/jsp/count.jsp").forward(request,response);
    }

}
